using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

using libsecondlife;

using Feathertail;
using Feathertail.Tracker;
using Feathertail.Tracker.Filters;

namespace NestBox
{
    class Follow : Feathertail.Command
    {
        private LLUUID _PreyUUID = LLUUID.Zero;
        private string _FilterName;

        private FeathertailClient _Client;
        private ObjectOverlord _ObjectTracker;

        #region Command Members

        public void Setup(FeathertailClient Client)
        {
            _Client = Client;
            _ObjectTracker = ((ObjectOverlord)_Client.Objects);
            _ObjectTracker.OnRunningFilterHit += new ObjectOverlord.OnRunningFilterHitCallback(objOverlord_OnRunningFilterHit);

        }

        public string HandleInput(string[] CmdLine)
        {
            if (CmdLine.Length < 2)
            {
                if (_PreyUUID != LLUUID.Zero)
                {
                    _PreyUUID = LLUUID.Zero;
                    _ObjectTracker.RunningFilterClear(_FilterName);
                    _Client.Self.AutoPilotLocal((int)_Client.Self.Position.X, (int)_Client.Self.Position.Y, _Client.Self.Position.Z);
                    return "Target UUID cleared, no longer following.";
                }
                return GetHelpText();
            }

            if (!LLUUID.TryParse(CmdLine[1], out _PreyUUID))
            {
                return "Invalid UUID specified: " + CmdLine[1];
            }

            UUIDFilter PreyFilter = new UUIDFilter(_PreyUUID);
            List<ObjectFilter> PreyFilters = new List<ObjectFilter>();
            PreyFilters.Add(PreyFilter);
            List<LLObject> ScanResults = _ObjectTracker.FilterObjects(PreyFilters);

            if (ScanResults.Count == 0)
            {
                return "Could not find: " + _PreyUUID.ToStringHyphenated();
            }

            _FilterName = "follow-" + LLUUID.Random();
            _ObjectTracker.RunningFilterSet(_FilterName, PreyFilters);

            _Client.Self.AutoPilotLocal((int)ScanResults[0].Position.X, (int)ScanResults[0].Position.Y, ScanResults[0].Position.Z);

            return "Attempting to follow: " + _PreyUUID.ToStringHyphenated();
        }

        void objOverlord_OnRunningFilterHit(Simulator simulator, string filterName, LLObject obj)
        {
            _Client.Self.AutoPilotLocal((int)obj.Position.X, (int)obj.Position.Y, obj.Position.Z);
        }

        public string[] GetCommandStrings()
        {
            return new string[] { "follow" };
        }

        public string GetSummaryText()
        {
            return "Follow an avatar or object specified by UUID.";
        }

        public string GetHelpText()
        {
            return GetSummaryText()
                + Environment.NewLine + "Usage: FOLLOW [uuid] "
                + Environment.NewLine + "Example: FOLLOW f95aefc1b89c4b80aa47cae9a0300e10";
        }

        #endregion
    }
}
